import type { TaskStorage } from '../types/task';

const STORAGE_KEY = 'task-progress-viewer';

/**
 * 保存数据到 localStorage
 * @param data 任务数据
 */
export const saveToStorage = (data: TaskStorage): void => {
  try {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
  } catch (error) {
    console.error('Failed to save data to localStorage:', error);
    throw new Error('Failed to save data');
  }
};

/**
 * 从 localStorage 读取数据
 * @returns 任务数据或 null
 */
export const loadFromStorage = (): TaskStorage | null => {
  try {
    const data = localStorage.getItem(STORAGE_KEY);
    if (!data) return null;
    return JSON.parse(data) as TaskStorage;
  } catch (error) {
    console.error('Failed to load data from localStorage:', error);
    return null;
  }
};